package com.jinpeng.huobikline.scheduled;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import com.jinpeng.huobikline.common.JsonUtils;
import com.jinpeng.huobikline.common.RedisUtils;
import com.jinpeng.huobikline.common.ResDTO;
import com.jinpeng.huobikline.entity.DataDTO;
import com.jinpeng.huobikline.service.KlineServices;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


@Slf4j
@Component
public class ScheduledService {

    @Resource
    private KlineServices klineServices;

    @Resource
    private RedisUtils redisUtils;

    @Value("${game.getwinningnumbers}")
    private  String getwinningnumbers;
    @Value("${game.updateKline}")
    private  String updateKline;

    /**
     * 获取分钟线
     */
    @Scheduled(cron = "0/2 * * * * ?")
    public void oneKLineScheduled(){
        //log.info("=====>>>>>获取分钟线{}",System.currentTimeMillis());
        String o = (String)redisUtils.get("btcusdt:1:kline:endtime");
        if (StrUtil.isBlank(o)){
            klineServices.oneKMinLineScheduled("btcusdt", 2000);
        }else {
            klineServices.oneKMinLineScheduled("btcusdt", 2);
        }


    }

    /**
     * 获取五分钟线
     */
    @Scheduled(cron = "2 0/5 * * * ?")
    public void updateFivesKLine() {
        log.info("=====>>>>>获取五分钟线{}",System.currentTimeMillis());
        //当前时间减5的时间戳
        String format1 = DateUtil.format(DateUtil.offsetMinute(new Date(),-5), DatePattern.NORM_DATETIME_MINUTE_PATTERN);
        DateTime parse = DateUtil.parse(format1,DatePattern.NORM_DATETIME_MINUTE_PATTERN);
        Map o =  redisUtils.hmget("btcusdt:5:kline");
        String substring = String.valueOf(parse.getTime()).substring(0, 10);
        DataDTO o1 = JsonUtils.toBean(JsonUtils.toString(o.get(substring)), DataDTO.class);
        //获取开奖号码
        Map datajson = new HashMap();
        datajson.put("dateTime",substring);
        String updateClose = HttpRequest.post(getwinningnumbers).body(JsonUtils.toString(datajson)).header(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue()).execute().body();
        log.info("============================================获取当前开奖,时间为：{},数字为:{}",substring,updateClose);
        ResDTO resDTO =  JsonUtils.toBean(updateClose, ResDTO.class);
        if (!Objects.isNull(resDTO.getData())){
            String close = String.valueOf(o1.getClose());
            char[] chars = close.toCharArray();
            chars[4] = String.valueOf(resDTO.getData()).charAt(0);
            o1.setClose(Double.valueOf(String.valueOf(chars)));
            datajson.put("price",String.valueOf(chars));
            try {
                HttpRequest.post(updateKline).body(JsonUtils.toString(datajson)).header(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue()).execute().body();
            } catch (HttpException e) {
                log.error("更新数据失败，具体原因为：{}",e.getMessage());
            } finally {
                //更新对应K线
                Map map = new HashMap();
                map.put(substring,JsonUtils.toString(o1));
                redisUtils.hmset("btcusdt:5:kline:update",map);
                redisUtils.hmset("btcusdt:5:kline",map);
                log.info("==========================================当期K线已更新,更细k线数据为{}",map);
            }
        }

    }
    /**
     * 获取五分钟线
     */
    @Scheduled(cron = "0/10 * * * * ?")
    public void FivesKLineScheduled() {
        //log.info("=====>>>>>获取五分钟线{}",System.currentTimeMillis());
        String o = (String)redisUtils.get("btcusdt:5:kline:endtime");
        if (StrUtil.isBlank(o)){
            klineServices.fivesMinKLineScheduled("btcusdt", 2000);
        }else {
            klineServices.fivesMinKLineScheduled("btcusdt", 1);
        }
    }
    /**
     * 获取十五分钟线
     */
    @Scheduled(cron = "0 0/15 * * * ?")
    public void fifteenKLineScheduled() {
        //log.info("=====>>>>>获取十五分钟线{}",System.currentTimeMillis());
        String o = (String)redisUtils.get("btcusdt:15:kline:endtime");
        if (StrUtil.isBlank(o)){
            klineServices.fifteenMinKLineScheduled("btcusdt", 2000);
        }else {
            klineServices.fifteenMinKLineScheduled("btcusdt",1);
        }

    }

    /**
     * 获取三十分钟线
     */
    @Scheduled(cron = "0 0/30 * * * ?")
    public void thirtyKLineScheduled() {
        //log.info("=====>>>>>获取三十分钟线{}",System.currentTimeMillis());
        String o = (String)redisUtils.get("btcusdt:30:kline:endtime");
        if (StrUtil.isBlank(o)){
            klineServices.thirtyMinKLineScheduled("btcusdt", 2000);
        }else {
            klineServices.thirtyMinKLineScheduled("btcusdt", 1);
        }

    }

    /**
     * 获取小时
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void oneHourScheduled() {
        //log.info("=====>>>>>获取小时{}",System.currentTimeMillis());
        String o = (String) redisUtils.get("btcusdt:60:kline:endtime");
        if (StrUtil.isBlank(o)){
            klineServices.oneHourScheduled("btcusdt", 2000);
        }else {
            klineServices.oneHourScheduled("btcusdt",1);
        }
    }

    /**
     * 获取四小时
     */
    @Scheduled(cron = "0 0 0/4 * * ?")
    public void fourHourScheduled() {
       // log.info("=====>>>>>获取四小时{}",System.currentTimeMillis());
        String o = (String)redisUtils.get("btcusdt:4hour:kline:endtime");
        if (StrUtil.isBlank(o)){
            klineServices.fourHourScheduled("btcusdt", 2000);
        }else {
            klineServices.fourHourScheduled("btcusdt",1);
        }
    }

    /**
     * 获取日线
     */
    @Scheduled(cron = "0 0 1 1/1 * ?")
    public void oneDayScheduled() {
        //log.info("=====>>>>>获取日线{}",System.currentTimeMillis());
        String o = (String)redisUtils.get("btcusdt:oneDay:kline:endtime");
        if (StrUtil.isBlank(o)){
            klineServices.oneDayScheduled("btcusdt", 2000);
        }else {
            klineServices.oneDayScheduled("btcusdt", 1);
        }
    }

    /**
     * 获取月线
     */
    @Scheduled(cron = "0 0 2 1/2 * ?")
    public void oneMonScheduled() {
        //log.info("=====>>>>>获取月线{}",System.currentTimeMillis());
        String o = (String) redisUtils.get("btcusdt:onemon:kline:endtime");
        if (StrUtil.isBlank(o)){
            klineServices.oneMonScheduled("btcusdt", 2000);
        }else {
            klineServices.oneMonScheduled("btcusdt", 1);
        }
    }


}
